package route

import (
	"time"

	"gitee.com/jiebaiyou/formflow/api/middleware"
	"gitee.com/jiebaiyou/formflow/bootstrap"
	"gitee.com/jiebaiyou/formflow/pkg/storage/redis"
	"gitee.com/jiebaiyou/formflow/pkg/storage/sqlite"
	"github.com/gin-gonic/gin"
)

// RegisterRoutes 注册所有API路由
func RegisterRoutes(env *bootstrap.Config, timeout time.Duration, db sqlite.DB, cache redis.Database, engine *gin.Engine) {
	// 添加全局中间件
	engine.Use(gin.Recovery())
	engine.Use(middleware.LoggerMiddleware())
	engine.Use(middleware.CorsMiddleware())

	// 初始化认证中间件
	authMiddleware := middleware.JwtAuthMiddleware(env.JWT.ACCESS_TOKEN_SECRET)

	// 系统路由 - 提供系统信息和健康检查
	systemRoutes := engine.Group("/api")
	NewSystemRouter(env, timeout, db, cache, systemRoutes)

	// 注册前端路由（处理静态资源、根路由和SPA路由）
	NewFrontendRouter(env, timeout, db, cache, engine)

	// 认证模块 - 公开API
	authRouter := engine.Group("/api/auth")
	NewSignupRouter(env, timeout, db, cache, authRouter)       // 用户注册
	NewLoginRouter(env, timeout, db, cache, authRouter)        // 用户登录
	NewRefreshTokenRouter(env, timeout, db, cache, authRouter) // 刷新令牌
	// 认证模块 - 受保护API
	authProtectedRouter := engine.Group("/api/auth")
	authProtectedRouter.Use(authMiddleware)
	NewProfileRouter(env, timeout, db, cache, authProtectedRouter) // 用户资料

	// 题库管理
	questionsRouter := engine.Group("/api/questions")
	questionsRouter.Use(authMiddleware)
	NewQuestionRouter(env, timeout, db, cache, questionsRouter)
}
